<?php defined('SYSPATH') or die('No direct script access.');

// -- Environment setup --------------------------------------------------------

// Load the core Kohana class
require SYSPATH.'classes/kohana/core'.EXT;

if (is_file(APPPATH.'classes/kohana'.EXT))
{
	// Application extends the core
	require APPPATH.'classes/kohana'.EXT;
}
else
{
	// Load empty core extension
	require SYSPATH.'classes/kohana'.EXT;
}

/**
 * Set the default time zone.
 *
 * @see  http://kohanaframework.org/guide/using.configuration
 * @see  http://php.net/timezones
 */
date_default_timezone_set('Europe/Moscow');

/**
 * Set the default locale.
 *
 * @see  http://kohanaframework.org/guide/using.configuration
 * @see  http://php.net/setlocale
 */
setlocale (LC_ALL, 'ru_RU.utf-8');

/**
 * Enable the Kohana auto-loader.
 *
 * @see  http://kohanaframework.org/guide/using.autoloading
 * @see  http://php.net/spl_autoload_register
 */
spl_autoload_register(array('Kohana', 'auto_load'));

/**
 * Enable the Kohana auto-loader for unserialization.
 *
 * @see  http://php.net/spl_autoload_call
 * @see  http://php.net/manual/var.configuration.php#unserialize-callback-func
 */
ini_set('unserialize_callback_func', 'spl_autoload_call');

// -- Configuration and initialization -----------------------------------------

/**
 * Set the default language
 */
I18n::lang('ru');

/**
 * Set Kohana::$environment if a 'KOHANA_ENV' environment variable has been supplied.
 *
 * Note: If you supply an invalid environment name, a PHP warning will be thrown
 * saying "Couldn't find constant Kohana::<INVALID_ENV_NAME>"
 */
if (isset($_SERVER['KOHANA_ENV']))
{
	Kohana::$environment = constant('Kohana::'.strtoupper($_SERVER['KOHANA_ENV']));
}

/**
 * Initialize Kohana, setting the default options.
 *
 * The following options are available:
 *
 * - string   base_url    path, and optionally domain, of your application   NULL
 * - string   index_file  name of your index file, usually "index.php"       index.php
 * - string   charset     internal character set used for input and output   utf-8
 * - string   cache_dir   set the internal cache directory                   APPPATH/cache
 * - boolean  errors      enable or disable error handling                   TRUE
 * - boolean  profile     enable or disable internal profiling               TRUE
 * - boolean  caching     enable or disable internal caching                 FALSE
 */
Kohana::init(array(
	'base_url'   => '/',
	'index_file' => false
));

/**
 * Attach the file write to logging. Multiple writers are supported.
 */
Kohana::$log->attach(new Log_File(APPPATH.'logs'));

/**
 * Attach a file reader to config. Multiple readers are supported.
 */
Kohana::$config->attach(new Config_File);

/**
 * Cookie salt.
 */
Cookie::$salt = 'gfsdhhGfdg^fdshh';

/**
 * Enable modules. Modules are referenced by a relative or absolute path.
 */
Kohana::modules(array(
	 'auth'       => MODPATH.'auth',       // Basic authentication
	 'cache'      => MODPATH.'cache',      // Caching with multiple backends
	// 'codebench'  => MODPATH.'codebench',  // Benchmarking tool
	 'database'   => MODPATH.'database',   // Database access
	 'image'      => MODPATH.'image',      // Image manipulation
	 'orm'        => MODPATH.'orm',        // Object Relationship Mapping
	// 'unittest'   => MODPATH.'unittest',   // Unit testing
	// 'userguide'  => MODPATH.'userguide',  // User guide and API documentation
    // 'sitemap'    => MODPATH.'sitemap',
    // 'morphy'     => MODPATH.'morphy'
	));

/**
 * Set the routes. Each route must have a minimum of a name, a URI and a set of
 * defaults for the URI.
 */
 
Route::set('register', 'register',
    array
    ())
    ->defaults(array(
        'directory'  => 'index',
        'controller' => 'login',
        'action'     => 'register'
    ));
 
Route::set('login', 'login',
    array
    ())
    ->defaults(array(
        'directory'  => 'index',
        'controller' => 'login',
        'action'     => 'login'
    ));
    
Route::set('logout', 'logout',
    array
    ())
    ->defaults(array(
        'directory'  => 'index',
        'controller' => 'login',
        'action'     => 'logout'
    ));
    
Route::set('my', 'my(/<action>)')
    ->defaults(array(
        'directory'  => 'index',
        'controller' => 'my',
        'action'     => 'index'
    ));
    
Route::set('info', 'info(/<action>)')
    ->defaults(array(
        'directory'  => 'index',
        'controller' => 'info',
        'action'     => 'index'
    ));
 
Route::set('catalog-redir', 'catalog',
    array
    ())
    ->defaults(array(
        'directory'  => 'index',
        'controller' => 'catalog',
        'action'     => 'redir'
    ));

Route::set('catalog-index', 'catalog(/category/<category>)(/brand/<brand>)(/color/<color>)(/composition/<composition>)(/season/<season>)(/page/<page>)',
    array
    (
        'category'      => '[0-9a-zA-Z-_]+',
        'brand'         => '[0-9a-zA-Z-_]+',
        'color'         => '[0-9a-zA-Z-_]+',
        'composition'   => '[0-9a-zA-Z-_]+',
        'season'        => '[0-9a-zA-Z-_]+',
        'page'          => '[0-9]+',
    ))
    ->defaults(array(
        'directory'  => 'index',
        'controller' => 'catalog',
        'action'     => 'index'
    )); 
    
Route::set('product-go', 'product/go/<product_id>',
    array
    (
        'product_id' => '[0-9]+',
    ))
    ->defaults(array(
        'directory'  => 'index',
        'controller' => 'product',
        'action'     => 'go'
    ));
    
Route::set('product', 'product/<id>-<url>',
    array
    (
        'id'   => '[0-9]+',
        'url'  => '[0-9a-zA-Z-_]+',
    ))
    ->defaults(array(
        'directory'  => 'index',
        'controller' => 'product',
        'action'     => 'index'
    ));   
    
Route::set('coupon-product-go', 'coupon/go/<coupon_id>/<product_id>',
    array
    (
        'coupon_id'  => '[0-9]+',
        'product_id' => '[0-9]+',
    ))
    ->defaults(array(
        'directory'  => 'index',
        'controller' => 'coupon',
        'action'     => 'product_go'
    ));
    
Route::set('coupon-go', 'coupon/go/<coupon_id>',
    array
    (
        'coupon_id'  => '[0-9]+',
    ))
    ->defaults(array(
        'directory'  => 'index',
        'controller' => 'coupon',
        'action'     => 'go'
    ));
 
Route::set('coupon-product-head', 'coupon/head/<coupon_id>/<product_id>',
    array
    (
        'coupon_id'  => '[0-9]+',
        'product_id' => '[0-9]+',
    ))
    ->defaults(array(
        'directory'  => 'index',
        'controller' => 'coupon',
        'action'     => 'product_head'
    ));
    
Route::set('coupon-head', 'coupon/head/<coupon_id>',
    array
    (
        'coupon_id'  => '[0-9]+',
    ))
    ->defaults(array(
        'directory'  => 'index',
        'controller' => 'coupon',
        'action'     => 'head'
    ));
    
Route::set('shop-all', 'shop/all',
    array
    ())
    ->defaults(array(
        'directory'  => 'index',
        'controller' => 'shop',
        'action'     => 'all'
    ));
    
Route::set('shop', 'shop/<shop>',
    array
    (
        'shop' => '[0-9a-z-_]+',
    ))
    ->defaults(array(
        'directory'  => 'index',
        'controller' => 'shop',
        'action'     => 'index'
    ));
    
Route::set('shop-go', 'shop/go/<shop_id>',
    array
    (
        'shop_id'  => '[0-9]+',
    ))
    ->defaults(array(
        'directory'  => 'index',
        'controller' => 'shop',
        'action'     => 'go'
    ));
    
Route::set('brand-all', 'brand',
    array
    ())
    ->defaults(array(
        'directory'  => 'index',
        'controller' => 'brand',
        'action'     => 'index'
    ));
    
Route::set('brand-letter', 'brand/letter/<letter>',
    array
    (
        'letter' => '[^/]{1}', 
    ))
    ->defaults(array(
        'directory'  => 'index',
        'controller' => 'brand',
        'action'     => 'letter'
    ));
    
Route::set('manual', 'manual',
    array())
    ->defaults(array(
        'directory'  => 'index',
        'controller' => 'manual',
        'action'     => 'index'
    ));
    
Route::set('search', 'search/<query>(/page/<page>)',
    array
    (
        'query' => '[^/]+',
        'page'  => '[0-9]+',
    ))
    ->defaults(array(
        'directory'  => 'index',
        'controller' => 'index',
        'action'     => 'search'
    ));
    
Route::set('landing-brand-go', 'landing/brand/go/<brand>(/<category>)',
    array
    (
        'category' => '[^/]+',
        'brand'    => '[^/]+',
    ))
    ->defaults(array(
        'directory'  => 'index',
        'controller' => 'landing',
        'action'     => 'brand_go'
    ));
    
Route::set('landing-brand', 'landing/brand/<brand>/<category>',
    array
    (
        'category' => '[^/]+',
        'brand'    => '[^/]+',
    ))
    ->defaults(array(
        'directory'  => 'index',
        'controller' => 'landing',
        'action'     => 'brand'
    ));
    
Route::set('landing', 'landing/<action>',
    array
    ())
    ->defaults(array(
        'directory'  => 'index',
        'controller' => 'landing',
        'action'     => 'index'
    ));
    
Route::set('error', 'error/<action>(/<message>)',
    array(
        'action' => '[0-9]++',
        'message' => '.+',
    ))
    ->defaults(array(
        'controller' => 'error',
    ));

Route::set('default', '(<directory>(/<controller>(/<action>(/<id>))))')
	->defaults(array(
        'directory'  => 'index',
		'controller' => 'index',
		'action'     => 'index'
	));
